// .........................................................................
// Title: Master_Analysis.do
//
// Overall context manager for Stata build jobs
// .........................................................................

* ---------------------------------------
* Set up environment
* ---------------------------------------

* main globals
global whoami <WHOAMI>
global code_path <CODE_PATH>
global project_path <PROJECT_PATH>
global python_interpreter_path <PYTHON_INTERPRETER_PATH>

* paths
global raw "$project_path/raw"
global logs "$project_path/logs"
global tmp "$project_path/temp"
global temp "$project_path/temp"
global graphs "$project_path/graphs"
global output "$project_path/output"
global tables "$project_path/tables"
global holdings "$project_path/holdings"
global morningstar_hd "$output/morningstar_holding_detail"
global insurance_holdings "$project_path/insurance_master"

* create folders
cap mkdir $logs
cap mkdir $tmp
cap mkdir $temp
cap mkdir $graphs
cap mkdir $output
cap mkdir $tables
cap mkdir $holdings
cap mkdir $morningstar_hd
cap mkdir $insurance_holdings

* log file
cap mkdir "$logs/`2'"
cap log close 
log using "$logs/`2'/${whoami}_`2'.log", replace

* load parameters and programs
do $code_path/scripts/build_params.do
do $code_path/scripts/gen_duration_category.do

* scheme, excel settings
set scheme shplot
set excelxlsxlargefile on

* ---------------------------------------
* Launch job
* ---------------------------------------

if "`2'" == "Holdings_Sumstats" {
    do $code_path/analysis/holdings_sumstats.do
}

if "`2'" == "Portfolio_Composition_Sumstats" {
    do $code_path/analysis/portfolio_composition_sumstats.do
}

if "`2'" == "Coverage" {
    do $code_path/analysis/coverage.do
}

if "`2'" == "GZ_Events" {
    do $code_path/analysis/gz_events.do
}

if "`2'" == "Sale_Quantities" {
    do $code_path/analysis/sale_quantities.do
}

if "`2'" == "Flow_Dynamics" {
    do $code_path/analysis/flow_dynamics.do
}

if "`2'" == "Variation_Histograms" {
    do $code_path/analysis/variation_histograms.do
}

if "`2'" == "Idiosyncrasy" {
    do $code_path/analysis/idiosyncrasy.do
}

if "`2'" == "Persistence" {
    do $code_path/analysis/persistence.do
}

if "`2'" == "Liquidity_Dynamics" {
    do $code_path/analysis/liquidity_dynamics.do
}

if "`2'" == "Firm_Regressions" {
    do $code_path/analysis/firm_regressions.do
}

if "`2'" == "Plot_Estimates" {
    do $code_path/analysis/plot_estimates.do
}

if "`2'" == "Relationships" {
    do $code_path/analysis/relationships.do
}

if "`2'" == "Spreads" {
    do $code_path/analysis/spreads.do
}

cap log close
